package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Unit;
import com.itgaoshu.hospitalproject.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("unit")
public class UnitController {

    @Autowired
    private UnitService unitService;

    //查询所有计量的方法
    @RequestMapping("findAllUnit")
    @ResponseBody
    public Object findAllUnit(String unitName, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Unit> units = unitService.findAllUnit(unitName);
        PageInfo pageInfo = new PageInfo(units);
        Map<String,Object> tableData = new HashMap<>();
        tableData.put("msg","");
        tableData.put("code",0);
        tableData.put("data",pageInfo.getList());
        tableData.put("count",pageInfo.getTotal());
        return tableData;
    }


    //添加计量的方法
    @ResponseBody
    @RequestMapping("addUnit")
    public  Object addUnit(Unit unit){
        int count = unitService.count(unit);
        if(count==0){
            int result = unitService.addUnit(unit);
            if (result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return "该计量已经存在";
        }
    }
}